<?php defined('SYSPATH') or die('No direct script access.') ;
/**
 * 管理后台基础控制器
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: base.php 213 2012-05-24 09:33:22Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
abstract class Controller_Admin_Base extends Controller
{

    /**
     * @var Manager
     */
    public $manager = NULL;

    /**
     * @var Session_NameSpace
     */
    public $session = NULL;

    /**
     * Loads the [View] object.
     */
    public function before()
    {
        parent::before();

        $this->manager = Manager::instance();
        $this->session = Session_NameSpace::factory('admin');

        if ( ! $this->manager->is_logined() AND ! preg_match('#^/?admin/account/\w+#i', Request::detect_uri()))
        {
            if ( ! $this->request->is_ajax() AND $this->request->controller() != 'common')
            {
                $this->session->set('redirect', URL::current());
            }
            $this->request->redirect(URL::site('admin/account/login')); // 跳转到登录页面
        }

        // Set the global view variables
        View::set_global('manager', $this->manager);

        // Set the message helper view directory
        Helper_Message::$directory = 'admin/';
    }

    /**
     * 判断是否拥有访问权限
     *
     * @param  mixed  $privileges
     * @param  bool   $message
     * @return bool
     */
    public function privilege($privileges, $message = NULL)
    {
        if ( ! $this->manager->is_allow($privileges))
        {
            $message OR $message = __('no_privilege');
            if ($this->request->is_ajax())
            {
                exit($message);
            }
            else
            {
                $this->message($message)->render();
            }
        }
    }

} // End Controller_Admin_Base